import 'package:contacts_service/contacts_service.dart';
import 'package:dmz/base/pageWidget/base_stateful_widget.dart';
import 'package:dmz/base/widget/defult_text_field.dart';
import 'package:dmz/base/widget/defult_widget.dart';
import 'package:dmz/business/home/controller/share_moto_controller.dart';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class ShareVontactsWidgets {
  static mainListView(ShareMotoController controller) {
    Widget listSection(int index) {
      var key = controller.allKey[index];
      var datas = controller.contacts[key];
      return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
                alignment: Alignment.centerLeft,
                height: 71.sp,
                child: DefaultText(key, 35.sp, textColor: Color(0xFFF9F9F9))
                    .marginOnly(left: 32.sp)),
            Column(
                mainAxisSize: MainAxisSize.min,
                children: List<Widget>.generate(
                    datas!.length,
                    (index) => listItem(datas[index], index,
                        () => controller.turnNext(datas[index]))))
          ]);
    }

    return ListView.builder(
        controller: controller.scrollController,
        itemCount: controller.contacts.length,
        itemBuilder: (context, index) {
          return listSection(index);
        });
  }

  static Widget sidebarWidget(ShareMotoController controller) {
    item(data) {
      return Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  color: controller.keyedNow == data
                      ? Colors.red
                      : Colors.transparent,
                  borderRadius: BorderRadius.circular(72.sp)),
              width: 50.sp,
              height: 50.sp,
              child: DefaultText(data, 30.sp, textColor: Colors.white))
          .addGestureDetector(() => controller.didPageScrolling(data));
    }

    return GetBuilder<ShareMotoController>(
        id: 'sidebar',
        builder: (controllered) {
          return Column(
              mainAxisSize: MainAxisSize.min,
              children: controllered.allKey.map((e) => item(e)).toList());
        });
  }

  static Widget listItem(Contact data, int index, Function() click) {
    return Container(
        alignment: Alignment.centerLeft,
        padding: EdgeInsets.only(left: 36.sp),
        color: const Color(0xFF434650),
        child: Column(children: [
          Container(
              margin: EdgeInsets.only(right: 32.sp),
              height: 2.sp,
              color: index == 0 ? Colors.transparent : Color(0x4DD8D8D8)),
          Container(
              alignment: Alignment.centerLeft,
              height: 95.sp,
              child: DefaultText(data.displayName ?? '', 35.sp,
                  textColor: const Color(0xFFF9F9F9))),
        ])).addGestureDetector(click);
  }

  static Widget searchWidget(
    Function(String) onChanged,
  ) {
    return Container(
        alignment: Alignment.center,
        margin: EdgeInsets.only(
            top: 14.sp, bottom: 14.sp, left: 36.sp, right: 36.sp),
        height: 72.sp,
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(72.sp)),
        padding: EdgeInsets.only(left: 34.sp, right: 34.sp),
        child: Row(
          children: [
            Image.asset('assets/images/home/share/search_icon.png',
                width: 46.sp, height: 46.sp),
            Expanded(
                child: Container(
                    padding: EdgeInsets.only(top: 30.sp),
                    color: Colors.transparent,
                    child: DefultTextField(
                      32.sp,
                      hintText: '搜索联系人'.tr,
                      hitColor: Color(0xFF797A80),
                      textColor: Colors.black,
                      onChanged: onChanged,
                    ))),
            Image.asset('assets/images/home/share/people_icon.png',
                width: 46.sp, height: 46.sp)
          ],
        ));
  }

  static Widget searchResultWidet() {
    return GetBuilder<ShareMotoController>(
        id: 'searchList',
        builder: (controller) {
          return ListView.builder(
              itemCount: controller.searchDatas.length,
              itemBuilder: (context, index) {
                return listItem(controller.searchDatas[index], index,
                    () => controller.turnNext(controller.searchDatas[index]));
              });
        });
  }
}
